/**
 * 实现两种参数初始化窗口
 * 	1.对象——easyUI Window对象参数	例如：{collapsible:true}
 * 		destroy:true 当窗口关闭时会自动销毁
 * 		parent:parent 在父窗口创建窗口
 * 	2.方法——刷新父窗口时默认调用的方法
 */
(function(window, undefined){
	function createCloseEvent(win, iframe, callback){
		return function(){
			if(win.window("options").destroy){
				win.panel("destroy");
			}else{
				setTimeout(function(){
					iframe.attr('src', 'about:blank');
				},1);
			}
			if(typeof(callback)=="function") callback.call(win);
		};
	}
	var jwin = function(setting, callback){
		if(typeof(setting) == "function"){
			callback = setting;
			setting = {};
		}
		return new jwin.prototype.init(setting, callback);
	};
	jwin.fn = jwin.prototype = {
		init: function(setting, callback){
			var opt = {}, $, iframe, win;
			if(setting.parent&&setting.parent.window&&setting.parent.window.jQuery){
				$ = this.jQuery = setting.parent.window.jQuery;
				this.document = setting.parent.document;
			}else{
				$ = this.jQuery = window.jQuery;
				this.document = window.document;
			}
			this.container = this.document.body;
			if(typeof(callback) == "function") this.defaultFun = callback;
			
			opt = $.extend({}, this.defaults, setting);
			win = this.win = $(this.html).appendTo(this.container).window(opt);
			iframe = this.iframe = $('<iframe src="" frameborder="0" width="100%" height="100%"></iframe>').appendTo(win);
			win.window("options").onClose=createCloseEvent(win, iframe, opt.onClose);
			return this;
		},
		/**
		 * 显示窗口的方法
		 * @param {} title	窗口标题
		 * @param {} href	链接地址
		 * @param {} fun	方法：刷新父窗口的方法	不传递则调用默认方法
		 * 					对象：为调用默认刷新方法的参数
		 */
		show: function(setting, fun){
			this.reSize();
			if(typeof(fun) == "function") this.callback = fun;
			else this.argument = fun;
			this.iframe.attr('src', CONTEXT_PATH+(setting.href?setting.href:"")).focus();
			if(setting.onLoad)this.iframe.load(setting.onLoad);
			var defOpt = {href:null,title:FsTools.escapeHtmlTag(setting.title||""),onClose:this.win.window("options").onClose};
			if(typeof(setting.onClose)=="function") defOpt.onClose = createCloseEvent(this.win,this.iframe,setting.onClose);
			this.win.window(this.jQuery.extend({closed: false},setting,defOpt));
		},
		/**
		 * 重置窗口大小
		 * @param {} w 数字或百分比 默认50%
		 * @param {} h 数字或百分比 默认50%
		 */
		reSize: function(w, h){
			var _width = Math.max(this.document.documentElement.clientWidth, this.document.body.clientWidth),
				_height = (0 == this.document.documentElement.clientHeight ? this.jQuery(this.document).height() : this.document.documentElement.clientHeight),
				_sl = Math.max(this.document.documentElement.scrollLeft, this.document.body.scrollLeft),
				_st = Math.max(this.document.documentElement.scrollTop, this.document.body.scrollTop);
			if(arguments.length>1){
				if(typeof(w)=="number"){
				}else if(w.toString().lastIndexOf('%')){
					w = parseInt(w)*0.01*_width;
				}else{
					w = parseInt(w);
				}
				if(typeof(h)=="number"){
				}else if(h.toString().lastIndexOf('%')){
					h = parseInt(h)*0.01*_height;
				}else{
					h = parseInt(h);
				}
				w = w > _width ? _width : w;
				h = h > _height ? _height : h;
				this.win.window({width:w,height:h,left:_sl+(_width-w)*0.5,top:_st+(_height-h)*0.5});
			}else{
				this.win.window({width:_width*0.5,height:_height*0.5,left:_sl+_width*0.25,top:_st+_height*0.25});
			}
		},
		/**
		 * 缩放窗口大小
		 * @param {} w 数字或百分比 默认不变
		 * @param {} h 数字或百分比 默认不变
		 */
		zoomSize: function(w, h){
			var _width = this.jQuery(this.document).width(),
				_height = this.jQuery(this.document).height(),
				opt = this.win.window("options"),
				top = opt.top, left = opt.left;
			w = tran(opt.width, w);
			h = tran(opt.height, h);
			
			if((w + left) > _width) left = _width - w;
			if((h + top) > _height) top = _height - h;
			if(0 > left){
				left = 0;
				w = _width;
			}
			if(0 > top){
				top = 0;
				h = _height;
			}
			
			this.win.window({width:w,height:h,left:left,top:top});
			
			function tran(refer, val){
				if(val == undefined) return refer;
				if(typeof(val) == "number") return refer += val;
				if(typeof(val) == "string" && val.lastIndexOf("%"))
					return refer * 0.01 * parseInt(val);
				else return refer += parseInt(val);
			}
		},
		/**
		 * 关闭窗口
		 */
		hide: function(){
			this.win.window('close');
		},
		/**
		 * 刷新父窗口，一般为子页面调用刷新本窗口
		 * param 当该参数为空时自动传递show方法传递的参数
		 */
		refreshParent: function(param){
			if(param) this.argument = param;
			if(typeof(this.callback) == "function") this.callback.call(this, this.argument);
			else this.defaultFun.call(this, this.argument);
			this.callback = null;
			this.hide();
		},
		win: null,										//window对象
		iframe: null,									//iframe对象
		html: "<div class=\"easyui-window\"></div>",	//
		jQuery: null,									//
		container: null,								//容器
		document: null,									//
		argument: null,
		callback: null,									//回调函数
		defaultFun: function(){},						//默认回调函数
		defaults: {										//默认window配置
			title: "",
			iconCls: "",
			closed: true,
			minimizable: false,
			maximizable: false,
			collapsible: false,
			modal: true
		}
	};
	jwin.fn.init.prototype = jwin.fn;
	window.Jwindow = jwin;
})(window);